package activities;

import helper.CustomScrollView;
import helper.CustomScrollView.OnScrollStoppedListener;
import helper.Exercise;
import helper.ExercisesEnum;
import helper.GraphFactory;
import helper.GraphicalViewWrapper;
import helper.Settings;
import helper.StatsExpandableListAdapter;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;

import org.achartengine.GraphicalView;

import simpleDB.DatabaseHelper;
import simpleDB.StatisticDataObject;
import activities.StatisticsActivity.ViewState;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.intellifit.R;

public class StatisticsActivity2 extends Activity implements OnClickListener {

	public static double SWIPE_MIN_DISTANCE = 0.3;
	public static GestureDetector gDetector;

	List<GraphicalView> charts = new ArrayList<GraphicalView>();
	// the factory used to create graph charts
	GraphFactory sgraph;
	// the current graph
	public GraphicalView mChartView;
	public static int CHART_HEIGHT = 0;

	/* exercise selection and overlay fields */
	StatsExpandableListAdapter listAdapter;
	ExpandableListView expListView;
	List<String> listDataHeader;
	HashMap<String, List<String>> listDataChild;
	RelativeLayout overlay;

	/* variables holding current state */
	private CustomScrollView scrollView;

	private Exercise currentExercise;
	private ViewState currentViewState;
	private String selectedExercise;
	private SimpleDateFormat currDateFormat;
	private Calendar dateHelper;

	private DatabaseHelper _dbHelper;

	public static int nrOfCharts;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_statistics_2);

		// TODO last exercise selected??
		this.selectedExercise = ExercisesEnum.BICEP_CURLS.toString();
		this.currentViewState = ViewState.MONTH;
		currDateFormat = new SimpleDateFormat("MMMM yyyy");
		dateHelper = Calendar.getInstance();

		final RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);

		final RelativeLayout header = (RelativeLayout) findViewById(R.id.statHeader);
		overlay = (RelativeLayout) findViewById(R.id.overlay);

		// init onClick Listeners
		mainLayout.setOnClickListener(this);
		findViewById(R.id.statHeaderLeftBtn).setOnClickListener(this);
		findViewById(R.id.statHeaderRightBtn).setOnClickListener(this);
		findViewById(R.id.selectExerciseBtn).setOnClickListener(this);
		findViewById(R.id.selectExerciseBtn2).setOnClickListener(this);
		findViewById(R.id.statHeaderPeriod).setOnClickListener(this);
		findViewById(R.id.statHeaderMonth_Year).setOnClickListener(this);
		((TextView) findViewById(R.id.heading)).setText("STATISTICS "
				+ selectedExercise);
		((TextView) findViewById(R.id.statHeaderMonth_Year))
				.setText(currDateFormat.format(dateHelper.getTime())
						.toUpperCase());
		((TextView) findViewById(R.id.hlperTxtView)).setText(currDateFormat
				.format(dateHelper.getTime()).toUpperCase());

		header.post(new Runnable() {

			@Override
			public void run() {

				RelativeLayout.LayoutParams params = null;
				if (Settings.DISPLAY_HEIGHT <= 800) {
					CHART_HEIGHT = (int) (Settings.DISPLAY_HEIGHT * 0.5);
					params = new RelativeLayout.LayoutParams(
							Settings.DISPLAY_WIDTH,
							(int) (Settings.DISPLAY_HEIGHT * 0.5) - 50);
				} else {
					CHART_HEIGHT = (int) (Settings.DISPLAY_HEIGHT * 0.6);
					params = new RelativeLayout.LayoutParams(
							Settings.DISPLAY_WIDTH,
							(int) (Settings.DISPLAY_HEIGHT * 0.4));
				}

				header.setLayoutParams(params);
				mainLayout.updateViewLayout(header, params);

			}
		});

		overlay.post(new Runnable() {

			@Override
			public void run() {

				RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
						(int) (Settings.DISPLAY_WIDTH * 0.6),
						(int) (Settings.DISPLAY_HEIGHT * 0.6));
				ImageButton b = (ImageButton) findViewById(R.id.selectExerciseBtn);
				int[] ar = new int[2];
				b.getLocationInWindow(ar);
				// Log.i("GMM",""+ar[1] + " "+ ar[0]);
				// Log.i("GMM",""+b.getHeight());
				if (ar != null && ar.length >= 2) {
					params.setMargins(0, ar[1] - b.getHeight() + 20, 0, 0);
				}
				overlay.setLayoutParams(params);
				((ViewGroup) findViewById(R.id.totalLayout)).updateViewLayout(
						overlay, params);

			}
		});
		scrollView = (CustomScrollView) findViewById(R.id.container);
		scrollView.setActivity(this);
		scrollView.setClickable(false);
		scrollView.setFocusable(false);
		scrollView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				dismissExerciseSelectionOverlay();

				if (event.getAction() == MotionEvent.ACTION_UP) {
					scrollView.startScrollerTask();
				}
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
				}
				return false;
			}
		});

		scrollView.setOnScrollStoppedListener(new OnScrollStoppedListener() {

			public void onScrollStopped() {
				int scrollDist = -1;
				int curr = scrollView.getCurrPosX();
				int currChild = scrollView.getCurrChild();
				int scrollDir = scrollView.getCurrScrollDirection();
				if (!CustomScrollView.FLING_HANDLED) {
					scrollView.smoothScrollTo(
							(currChild) * scrollView.getWidth(), 0);
				}

				if (scrollDir > 0) {
					int distToCheck = 0;
					if (currChild == 0) {
						distToCheck = (int) (scrollView.getWidth() * SWIPE_MIN_DISTANCE);

					} else {
						distToCheck = (int) ((scrollView.getWidth() * (currChild)) + scrollView
								.getWidth() * SWIPE_MIN_DISTANCE);
					}

					if (curr >= distToCheck) {
						scrollView.increaseChildIndex();
						scrollDist = scrollView.getCurrChild()
								* scrollView.getWidth();
					} else {
						// flip back
						scrollView.smoothScrollTo(
								(currChild) * scrollView.getWidth(), 0);
						return;

					}
					scrollView.smoothScrollTo(scrollDist, 0);

				} else if (scrollDir < 0) {
					if (currChild - 1 <= 0) {
						if (curr >= scrollView.getWidth()
								* (1 - SWIPE_MIN_DISTANCE)) {
							scrollView.decreaseChildIndex();
							scrollDist = scrollView.getCurrChild()
									* scrollView.getWidth();
						}
					} else {
						if (curr <= (scrollView.getWidth() * (currChild - 1) + (scrollView
								.getWidth() * (1 - SWIPE_MIN_DISTANCE)))) {
							scrollView.decreaseChildIndex();
							scrollDist = scrollView.getCurrChild()
									* scrollView.getWidth();
						} else {
							scrollView.smoothScrollTo(
									currChild * scrollView.getWidth(), 0);
							return;

						}
					}
					scrollView.smoothScrollTo(scrollDist, 0);
				}
			}
		});
		scrollView.post(new Runnable() {

			@Override
			public void run() {
				scrollView.measure(0, 0);
				nrOfCharts = scrollView.getMeasuredWidth()
						/ scrollView.getWidth();
				Log.i("SCROLL", "nr of charts: " + nrOfCharts);
			}
		});

		findViewById(R.id.selectExerciseBtn).setOnClickListener(this);

		this.sgraph = new GraphFactory(this);
		_dbHelper = new DatabaseHelper(this);

		checkIfCalled();
		
		updateView(true);
		if (Settings.DISPLAY_HEIGHT <= 800) {
			fixLayout();
		}

	}

	private void checkIfCalled() {
		if (getCallingActivity() != null) {
			// if this activity was called from the result view in the
			// repetitionsActivity
			// extract the current chosen exercise to be shown

			try {
				Bundle b = getIntent().getExtras();
				Object o = b.get("exercise_data");
				currentExercise = (Exercise) o;

				if (currentExercise.getName().equals("Biceps Curls")) {
					this.selectedExercise = ExercisesEnum.BICEP_CURLS
							.toString();
				} else if (currentExercise.getName().equals("Squat")) {
					this.selectedExercise = ExercisesEnum.SQUATS.toString();
				} else if (currentExercise.getName().equals("Sit ups")) {
					this.selectedExercise = ExercisesEnum.SIT_UPS.toString();
				} else {
					throw new NullPointerException();
				}

			} catch (NullPointerException e) {
				currentExercise = Settings.lastExerciseDone;
				this.selectedExercise = ExercisesEnum.BICEP_CURLS.toString();
			}
		}

	}

	private void fixLayout() {
		try {
			TableLayout l = (TableLayout) findViewById(R.id.tableLayout1);
			for (int i = 0; i < l.getChildCount(); i++) {
				TableRow row = (TableRow) l.getChildAt(i);
				for (int j = 0; j < row.getChildCount(); j++) {
					Object child = row.getChildAt(j);
					if (child instanceof TextView) {
						((TextView) child).setTextSize(13);
					}
				}
			}
		} catch (Exception e) {
			Log.e("exceptionRaised", e.getMessage());
		}
	}

	private void reInitStatisticFields() {
		// calculate total reps
		// calculate reps/day
		// calculate total Time
		double reps = 0;
		double accuracy = 0;
		double repsPerPeriod = 0;
		double duration = 0;

		int year = this.dateHelper.get(Calendar.YEAR);
		int month = this.dateHelper.get(Calendar.MONTH) + 1;
		int persID = Settings.getInstance().getPersonID();
		int exID = Settings.getInstance().getExerciseID(this.selectedExercise);
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(2);
		if (this.currentViewState == ViewState.MONTH) {
			StatisticDataObject stdo = _dbHelper
					.getRepetitionsForExerciseForMonth(persID, year, month,
							exID);
			reps = stdo.repetitions;
			accuracy = (double) stdo.accuracy / (double) reps;
			accuracy *= 100;
			duration = (double) stdo.duration / 1000.0 / 60.0;
			repsPerPeriod = reps / dateHelper.getMaximum(Calendar.DAY_OF_MONTH);
			((TextView) findViewById(R.id.totalTime)).setText(""
					+ (int) Math.round(duration) + " min");

		} else if (this.currentViewState == ViewState.YEAR) {
			ArrayList<StatisticDataObject> r = _dbHelper.getRepetionsPerMonth(
					persID, year, exID);
			this.sgraph.setDAO(r, currentViewState);
			for (StatisticDataObject integer : r) {
				accuracy += integer.accuracy;
				duration += integer.duration;
				reps += integer.repetitions;
			}
			accuracy = accuracy / reps;
			repsPerPeriod = reps / 12;
			accuracy *= 100;
			duration = (double) duration / 1000.0 / 60.0 / 60.0;
			((TextView) findViewById(R.id.totalTime)).setText(""
					+ nf.format(duration) + " h");
		}
		((TextView) findViewById(R.id.totalReps)).setText("" + (int) reps);

		((TextView) findViewById(R.id.repsPerDay)).setText(nf
				.format(repsPerPeriod));

		((TextView) findViewById(R.id.averageAccuracyTxtView)).setText(""
				+ (int) Math.round(accuracy) + "%");
	}

	@Override
	protected void onResume() {
		// initCharts(false);
		super.onResume();
	}

	private void initCharts() {

		LinearLayout l = (LinearLayout) findViewById(R.id.chartWrapper);

		sgraph.fetchStatisticDataObjects(currentViewState, selectedExercise,
				dateHelper);
		generateWorkoutTimeChart();
		generateRepetitionsChart();
		generateAccuracyChart();

		l.refreshDrawableState();

	}

	private void initListView() {
		// get the listview
		expListView = (ExpandableListView) findViewById(R.id.lvExp);

		// preparing list data
		prepareListData();

		listAdapter = new StatsExpandableListAdapter(this, listDataHeader,
				listDataChild);

		// setting list adapter
		expListView.setAdapter(listAdapter);

		// Listview Group click listener
		expListView.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				return false;
			}
		});

		// Listview Group expanded listener
		expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {

			}
		});

		// Listview Group collasped listener
		expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

			@Override
			public void onGroupCollapse(int groupPosition) {
			}
		});

		// Listview on child click listener
		expListView.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {

				Toast.makeText(
						getApplicationContext(),
						listDataHeader.get(groupPosition)
								+ " : "
								+ listDataChild.get(
										listDataHeader.get(groupPosition)).get(
										childPosition), Toast.LENGTH_SHORT)
						.show();
				selectedExercise = listDataChild.get(
						listDataHeader.get(groupPosition)).get(childPosition);

				updateView(true);
				dismissExerciseSelectionOverlay();
				return true;
			}

		});

	}

	protected void updateView(boolean updateTitle) {		
		if (updateTitle) {
			TextView t = (TextView) findViewById(R.id.heading);
			t.setText("STATISTICS " + selectedExercise);
		}		
		initCharts();
		reInitStatisticFields();
		updateColors();

		if (currentViewState.equals(ViewState.MONTH)) {
			((TextView) findViewById(R.id.statHeaderPeriod)).setText("MONTH");
			currDateFormat = new SimpleDateFormat("MMMM yyyy");
			((TextView) findViewById(R.id.statHeaderMonth_Year))
					.setText(currDateFormat.format(dateHelper.getTime())
							.toUpperCase(Locale.UK));
			((TextView) findViewById(R.id.hlperTxtView)).setText(currDateFormat
					.format(dateHelper.getTime()).toUpperCase(Locale.UK));
			((TextView) findViewById(R.id.repsPerDayTV)).setText("REPs / DAY");

		} else if (currentViewState.equals(ViewState.YEAR)) {
			((TextView) findViewById(R.id.statHeaderPeriod)).setText("YEAR");
			currDateFormat = new SimpleDateFormat("yyyy");
			((TextView) findViewById(R.id.statHeaderMonth_Year))
					.setText(currDateFormat.format(dateHelper.getTime())
							.toUpperCase(Locale.UK));
			((TextView) findViewById(R.id.hlperTxtView)).setText(currDateFormat
					.format(dateHelper.getTime()).toUpperCase(Locale.UK));
			((TextView) findViewById(R.id.repsPerDayTV))
					.setText("REPs / MONTH");
		}

	}

	private void updateColors() {
		// looks ugly
		// findViewById(R.id.tableLayout1).setBackgroundColor(Color.argb(255,
		// 32,33,6));
		// findViewById(R.id.statHeader).setBackgroundColor(Color.argb(255,
		// 34,34,18));

		((TextView) findViewById(R.id.heading)).setTextColor(Settings
				.getInstance().getExerciseColor(selectedExercise));
		((TextView) findViewById(R.id.repsPerDay)).setTextColor(Settings
				.getInstance().getExerciseColor(selectedExercise));
		((TextView) findViewById(R.id.totalTime)).setTextColor(Settings
				.getInstance().getExerciseColor(selectedExercise));
		((TextView) findViewById(R.id.totalReps)).setTextColor(Settings
				.getInstance().getExerciseColor(selectedExercise));
		((TextView) findViewById(R.id.averageAccuracyTxtView))
				.setTextColor(Settings.getInstance().getExerciseColor(
						selectedExercise));

	}

	public void dismissExerciseSelectionOverlay() {
		if (findViewById(R.id.totalLayout).getVisibility() != View.INVISIBLE) {
			ObjectAnimator oa = ObjectAnimator.ofFloat(overlay, "translationX",
					0f, (float) -1 * Settings.DISPLAY_WIDTH);
			oa.addListener(new AnimatorListenerAdapter() {
				@Override
				public void onAnimationEnd(Animator animation) {

					findViewById(R.id.selectExerciseBtn2).setVisibility(
							View.INVISIBLE);
					findViewById(R.id.totalLayout)
							.setVisibility(View.INVISIBLE);
				}

				@Override
				public void onAnimationStart(Animator animation) {
					findViewById(R.id.selectExerciseBtn).setVisibility(
							View.VISIBLE);
				}
			});
			oa.start();
		}
	}

	/**
	 * TODO load exercises from database??
	 */
	private void prepareListData() {
		listDataHeader = new ArrayList<String>();
		listDataChild = new HashMap<String, List<String>>();

		// Adding child data
		listDataHeader.add("BODY");
		listDataHeader.add("WEIGHT");
		listDataHeader.add("ENDURANCE");

		ArrayList<String> bwEx = new ArrayList<String>();
		bwEx.add("PUSH-UPs");
		bwEx.add("PULL-UPs");
		bwEx.add("SIT-UPs");
		bwEx.add("SQUATS");
		ArrayList<String> weEx = new ArrayList<String>();
		weEx.add("BICEP-CURLs");
		weEx.add("TRICEP-CURLs");
		weEx.add("BENCHPRESS");
		ArrayList<String> enEx = new ArrayList<String>();
		enEx.add("RUNNING");
		enEx.add("STEPPING");

		listDataChild.put(listDataHeader.get(0), bwEx); // Header, Child data
		listDataChild.put(listDataHeader.get(1), weEx);
		listDataChild.put(listDataHeader.get(2), enEx);
	}

	protected void createExerciseSelectionOverlay() {
		findViewById(R.id.totalLayout).setVisibility(View.VISIBLE);

		findViewById(R.id.selectExerciseBtn).setVisibility(View.INVISIBLE);

		findViewById(R.id.selectExerciseBtn2).setVisibility(View.VISIBLE);

		overlay.measure(0, 0);

		ObjectAnimator oa = ObjectAnimator.ofFloat(overlay, "translationX",
				(float) (-1 * (overlay.getMeasuredWidth() * 0.4)), 0f);
		oa.start();
		// get the listview
		expListView = (ExpandableListView) findViewById(R.id.lvExp);
		// findViewById(R.id.totalOverlay).setVisibility(View.VISIBLE);

		// preparing list data
		prepareListData();

		initListView();

	}

	protected void generateWorkoutTimeChart() {
		LinearLayout layout = (LinearLayout) findViewById(R.id.workoutTimeChart);
		layout.removeAllViews();
		layout = (LinearLayout) findViewById(R.id.workoutTimeChart);
		layout.removeAllViews();
		mChartView = sgraph.getWorkoutTimeChart(scrollView.getContext(),
				currentViewState);

		if (Settings.DISPLAY_HEIGHT <= 800) {
			layout.addView(mChartView, new LayoutParams(Settings.DISPLAY_WIDTH,
					(int) (Settings.DISPLAY_HEIGHT * 0.5)));

		} else {
			layout.addView(mChartView, new LayoutParams(Settings.DISPLAY_WIDTH,
					(int) (Settings.DISPLAY_HEIGHT * 0.6) + 10));
		}

		// this.charts.add(mChartView);
	}

	protected void generateAccuracyChart() {
		LinearLayout layout = (LinearLayout) findViewById(R.id.accuracyChart);
		layout.removeAllViews();
		mChartView = sgraph.getAccuracyChart(scrollView.getContext(),
				currentViewState);

		if (Settings.DISPLAY_HEIGHT <= 800) {
			layout.addView(mChartView, new LayoutParams(Settings.DISPLAY_WIDTH,
					(int) (Settings.DISPLAY_HEIGHT * 0.5)));
		} else {
			layout.addView(mChartView, new LayoutParams(Settings.DISPLAY_WIDTH,
					(int) (Settings.DISPLAY_HEIGHT * 0.6)));
		}
		// this.charts.add(mChartView);

	}

	protected void generateRepetitionsChart() {

		LinearLayout layout = (LinearLayout) findViewById(R.id.repsChart);
		layout.removeAllViews();
		final GraphicalViewWrapper mvw = sgraph.getRepetitionsChart(
				scrollView.getContext(), currentViewState);
		mChartView = mvw.getChart();
		// add the graph chart using the whole width and 60% of the display
		// height
		if (Settings.DISPLAY_HEIGHT <= 800) {
			layout.addView(mChartView, new LayoutParams(Settings.DISPLAY_WIDTH,
					(int) (Settings.DISPLAY_HEIGHT * 0.5)));
		} else {
			layout.addView(mChartView, new LayoutParams(Settings.DISPLAY_WIDTH,
					(int) (Settings.DISPLAY_HEIGHT * 0.6)));
		}

		mChartView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				double d[] = mChartView.toRealPoint(0);

				int day = (int) Math.round(d[0]);
				int re = (int) mvw.getYValueOfCurrentSeries(day);

				if (day >= 0
						&& day <= dateHelper
								.getActualMaximum(Calendar.DAY_OF_MONTH)
						&& re > 0) {
					if (StatisticsActivity2.this.currentViewState == ViewState.MONTH) {
						Toast.makeText(StatisticsActivity2.this,
								"YOU REACHED " + re + " REPS ON DAY " + ++day,
								Toast.LENGTH_SHORT).show();
					} else if (StatisticsActivity2.this.currentViewState == ViewState.YEAR) {
						if (day + 1 <= dateHelper.get(Calendar.MONTH) + 1) {
							Toast.makeText(
									StatisticsActivity2.this,
									"YOU REACHED "
											+ re
											+ " REPS IN "
											+ Settings.getInstance().monthMapping
													.get(day + 1),
									Toast.LENGTH_SHORT).show();
						}
					}
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		if (this.currentViewState.equals(ViewState.MONTH)
				|| this.currentViewState.equals(ViewState.YEAR)) {
			dateHelper.set(Calendar.HOUR_OF_DAY, 0);
			dateHelper.set(Calendar.MINUTE, 0);
			dateHelper.set(Calendar.SECOND, 0);
			dateHelper.set(Calendar.MILLISECOND, 0);
			dateHelper.set(Calendar.DAY_OF_MONTH, 1);
		}

		if (v == findViewById(R.id.selectExerciseBtn)) {
			createExerciseSelectionOverlay();
			return;
		} else if (v == findViewById(R.id.selectExerciseBtn2)) {
			dismissExerciseSelectionOverlay();
		} else if (v == findViewById(R.id.statHeaderLeftBtn)) {
			if (currentViewState.equals(ViewState.MONTH)) {
				Log.i("SCROLL", "" + dateHelper.get(Calendar.YEAR) + " "
						+ dateHelper.get(Calendar.MONTH));
				dateHelper.add(Calendar.MONTH, -1);
			} else if (currentViewState.equals(ViewState.YEAR)) {
				dateHelper.add(Calendar.YEAR, -1);
			}
			updateView(false);
		} else if (v == findViewById(R.id.statHeaderRightBtn)) {
			if (currentViewState.equals(ViewState.MONTH)) {
				dateHelper.add(Calendar.MONTH, 1);
			} else if (currentViewState.equals(ViewState.YEAR)) {
				dateHelper.add(Calendar.YEAR, 1);
			}
			updateView(false);
		} else if (v == findViewById(R.id.statHeaderMonth_Year)
				|| v == findViewById(R.id.statHeaderPeriod)) {
			if (currentViewState.equals(ViewState.MONTH)) {
				currentViewState = ViewState.YEAR;
				
			} else if (currentViewState.equals(ViewState.YEAR)) {
				currentViewState = ViewState.MONTH;				
			}
			updateView(false);
		} else {
			dismissExerciseSelectionOverlay();
		}

	}

}